<template>
  <el-dialog :title="$t('processDialog.submitTitle')"
             :visible.sync="visible"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             :before-close="close"
             append-to-body
             width="20%">
    <el-form ref="form"
             :model="form">
      <el-form-item label="提交内容"
                    :rules="[{required:true,message:'请输入提交内容',trigger:'submit'}]"
                    prop="content">
        <el-input v-model="form.content"
                  type="textarea"
                  placeholder="请输入提交内容..."
                  :rows="4"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-tooltip placement="top"
                  :enterable="false"
                  :content="$t('common.submit')">
        <el-button type="primary"
                   icon="el-icon-check"
                   @click="confirm"></el-button>
      </el-tooltip>
      <el-tooltip placement="top"
                  :enterable="false"
                  :content="$t('processDialog.cancel')">
        <el-button @click="close"
                   icon="el-icon-close"></el-button>
      </el-tooltip>
    </span>
  </el-dialog>
</template>

<script>
import { startFlow } from '../../api/processManagement/flowInstance'
import { mapGetters } from 'vuex'

export default {
  props: {
    tableName: {
      type: String,
      remark: '数据库表名（业务资源实体可以查到）'
    },
    funcTypeId: {
      type: [Number, String],
      remark: '类型ID、对接状态类型组件后自动写入',
      default: ''
    },
    objectId: {
      remark: '单据ID、必传',
      type: [Number, String]
    },
    reloadFunc: {
      type: Function,
      remark: '自定义刷新方法',
      default () {
        location.reload()
      }
    },
    routePrefix: {
      remark: '当前模块标识（自动获取、对接跨端组件需要传入）',
      type: String,
      default: ''
    }
  },
  data () {
    return {
      visible: false,
      form: {
        content: ''
      }
    }
  },
  computed: mapGetters(['currentModule', 'typeId']),
  methods: {
    confirm () {
      if (this.validatenull(this.objectId)) {
        this.$message.warning(this.$t('processDialog.errorConfig'))
        console.error('请传入objectId --> 单据对象id')
        return
      }
      this.$refs.form.validate(valid => {
        if (!valid) return
        const obj = {
          tableName: this.tableName,
          auditObjectList: Array.isArray(this.objectId) ? this.objectId.map(res => {
            return { funcTypeId: this.funcTypeId || this.typeId, objectId: res }
          }) : [{ funcTypeId: this.funcTypeId || this.typeId, objectId: this.objectId }],
          remark: this.form.content
        }
        startFlow(this.routePrefix || this.currentModule.routePrefix, obj).then(res => {
          this.$message({
            type: res.data.meta.success ? 'success' : 'error',
            message: res.data.meta.message
          })
          this.$emit('success', res)
          this.visible = false
          if (res.data.meta.success) {
            this.reloadFunc()
            this.$store.commit("SET_RELOADAUDITINFOMATION", true)
          }
        }, error => {
          this.visible = false
          this.reloadFunc()
          console.error(error)
        })
      })
    },
    close () {
      this.visible = false
    }
  }
}
</script>

<style>
</style>